package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DateUtil;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.GoodRxTimerUtil;
import com.jilian.mydemo.utils.MainRxTimerUtil;
import com.jilian.mydemo.utils.ZXingUtils;

/**
 * 支付 二维码
 */
public class PayQcodeActivity extends BaseActivity {
    private ImageView ivQCode;
    private TextView tvType;

    private TextView tvHour;
    private TextView tvMin;
    private TextView tvSecond;
    private TextView tvMoney;

    private OrderViewModel viewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
        MainRxTimerUtil.cancel();
        GoodRxTimerUtil.cancel();
    }


    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_payqcode;
    }

    @Override
    public void initView() {
        setNormalTitle("支付订单", v -> finish());
        ivQCode = (ImageView) findViewById(R.id.iv_q_code);
        tvType = (TextView) findViewById(R.id.tv_type);
        tvHour = (TextView) findViewById(R.id.tv_hour);
        tvMin = (TextView) findViewById(R.id.tv_min);
        tvSecond = (TextView) findViewById(R.id.tv_second);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        tvMoney.setText(getIntent().getStringExtra("totalMoney"));
        if ("1".equals(getIntent().getStringExtra("type"))) {
            tvType.setText("请使用支付宝扫描以下二维码");
        }
        if ("2".equals(getIntent().getStringExtra("type"))) {
            tvType.setText("请使用微信扫描以下二维码");
        }
        //开启定时器获取支付状态
        startTimePayTask();

    }

    /**
     * 检查支付状态
     */
    private void startTimePayTask() {
        GoodRxTimerUtil.interval(3000, new GoodRxTimerUtil.IRxNext() {
            @Override
            public void doNext() {//获取现在的 时分秒 时间戳

                viewModel.checkPayStatus(getIntent().getStringExtra("orderId"));
                viewModel.getCheckOrderliveData().observe(PayQcodeActivity.this, new Observer<BaseDto<String>>() {
                    @Override
                    public void onChanged(@Nullable BaseDto<String> baseDto) {
                        if (baseDto.isSuccess()) {

                            if ("1".equals(baseDto.getData())) {
                                //
                                MyApplication.clearSpecifyActivities(new Class[]{ThreeOrderDetaiPayActivity.class});
                                MyApplication.clearSpecifyActivities(new Class[]{
                                        ThreeOrderDetaiCommitelActivity.class});
                                MyApplication.clearSpecifyActivities(new Class[]{ThreeOrderDetailActivity.class});
                                MyApplication.clearSpecifyActivities(new Class[]{FinishOrderActivity.class,});
                                MyApplication.clearSpecifyActivities(new Class[]{InputServiceDataActivity.class,});
                                GoodRxTimerUtil.cancel();
                                Intent intent = new Intent(PayQcodeActivity.this, PaySuccessActivity.class);
                                intent.putExtra("type", getIntent().getStringExtra("type"));
                                intent.putExtra("totalMoney", getIntent().getStringExtra("totalMoney"));
                                intent.putExtra("orderId", getIntent().getStringExtra("orderId"));
                                intent.putExtra("orderDto", getIntent().getSerializableExtra("orderDto"));
                                startActivity(intent);
                                finish();

                            }
                        }
                    }
                });

            }
        });
    }

    private void startTimeTask(long delTime) {
        MainRxTimerUtil.interval(1000, new MainRxTimerUtil.IRxNext() {
            @Override
            public void doNext() {//获取现在的 时分秒 时间戳

                if (delTime > 0) {
                    String str = DateUtil.timeToHms(delTime);
                    tvHour.setText(str.split(":")[0]);
                    tvMin.setText(str.split(":")[1]);
                    tvSecond.setText(str.split(":")[2]);

                } else {
                    finish();
                }

            }
        });
    }

    @Override
    public void initData() {
        String url = getIntent().getStringExtra("url");
        Log.e(TAG, "url: " + url);
        Bitmap bitmap = ZXingUtils.createQRImage(url, DisplayUtil.dip2px(this, 230), DisplayUtil.dip2px(this, 230));
        ivQCode.setImageBitmap(bitmap);
    }

    @Override
    public void initListener() {
    }
}
